#!/usr/bin/env bash
#
# Helper script to install the non-Python Wayland dependencies on Ubuntu. This
# is required because most of the time the wlroots version we depend on is more
# recent than that which can be found in the Ubuntu package repository. This
# script is used for CI on GitHub and the ReadTheDocs environment.

set -e

mkdir -p cache


# The versions we want
WAYLAND=1.24.0
WAYLAND_PROTOCOLS=1.41
WLROOTS=0.19.0
SEATD=0.6.4
LIBDRM=2.4.122
PIXMAN=0.43.0
XWAYLAND=22.1.9
HWDATA=0.364


fetch_source() {
    url=$1
    filename=$2
    if [ -f "cache/$filename" ]; then
        echo "$filename found, skipping download."
    else
        wget -P cache "$url"
    fi
}


# Packaged dependencies
sudo apt update
sudo apt-get install -y --no-install-recommends \
     libepoxy-dev \
     libegl1-mesa-dev \
     libgbm-dev \
     libgles2-mesa-dev \
     libinput-dev \
     libpciaccess-dev \
     libxcb-composite0-dev \
     libxcb-dri3-dev \
     libxcb-ewmh-dev \
     libxcb-icccm4-dev \
     libxcb-image0-dev \
     libxcb-present-dev \
     libxcb-render0-dev \
     libxcb-res0-dev \
     libxcb-xfixes0-dev \
     libxcb-xinput-dev \
     libxcb1-dev \
     libxfont-dev \
     libxkbcommon-dev \
     libxshmfence-dev \
     libtirpc-dev \
     xfonts-utils \
     xserver-xorg-dev \
     ninja-build \
     meson

# Build wayland
tarball="wayland-$WAYLAND.tar.xz"
fetch_source "https://gitlab.freedesktop.org/wayland/wayland/-/releases/$WAYLAND/downloads/$tarball" "$tarball"
tar -xJf "cache/$tarball"
cd wayland-$WAYLAND
meson setup build -Ddocumentation=false --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build wayland-protocols
tarball="wayland-protocols-$WAYLAND_PROTOCOLS.tar.xz"
fetch_source "https://gitlab.freedesktop.org/wayland/wayland-protocols/-/releases/$WAYLAND_PROTOCOLS/downloads/$tarball" "$tarball"
tar -xJf "cache/$tarball"
cd wayland-protocols-$WAYLAND_PROTOCOLS
meson setup build -Dtests=false --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build libdrm
tarball="libdrm-libdrm-$LIBDRM.tar.gz"
fetch_source "https://gitlab.freedesktop.org/mesa/libdrm/-/archive/libdrm-$LIBDRM/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd libdrm-libdrm-$LIBDRM
meson setup build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build seatd
tarball="$SEATD.tar.gz"
fetch_source "https://github.com/kennylevinsen/seatd/archive/refs/tags/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd seatd-$SEATD
meson setup build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build pixman
tarball="pixman-pixman-$PIXMAN.tar.gz"
fetch_source "https://gitlab.freedesktop.org/pixman/pixman/-/archive/pixman-$PIXMAN/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd pixman-pixman-$PIXMAN
meson setup build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build hwdata
tarball="v$HWDATA.tar.gz"
fetch_source "https://github.com/vcrhonek/hwdata/archive/refs/tags/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd hwdata-$HWDATA
./configure --prefix=/usr --libdir=/lib --datadir=/usr/share
make
sudo make install
cd ../

# Build xwayland
tarball="xserver-xwayland-$XWAYLAND.tar.gz"
fetch_source "https://gitlab.freedesktop.org/xorg/xserver/-/archive/xwayland-$XWAYLAND/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd xserver-xwayland-$XWAYLAND
meson setup build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build wlroots
tarball="wlroots-$WLROOTS.tar.gz"
fetch_source "https://gitlab.freedesktop.org/wlroots/wlroots/-/archive/$WLROOTS/$tarball" "$tarball"
tar -xzf "cache/$tarball"
cd wlroots-$WLROOTS
meson setup build -Dexamples=false --prefix=/usr -Dxwayland=enabled
ninja -C build
sudo ninja -C build install
cd ../
